/*-------------------------------------------------
动态表格，可以动态添加和删除，并在表格中添加标签

2009-7-13   create by tss  
---------------------------------------------------*/

/**
 * 声明动态表格类
 */
var DyTable = function(id,obj_s){

	this.id = id;
	this.instance = document.getElementById(id);
	this.obj_s = obj_s;
	this.rows = new Array();
	this.delCallFun = null;
	var self = this;
	this.config = {
			
			tableClsName : "",
			trClsName : "",
			tdClsName : "",
			tdHtml : [],
			tdHtmlValue : null,
			autoDelOp : true,
		    chkIds : "chkId",
		    setDelCallFun : function(f){
		        self.delCallFun = f;
           	}
	};
};


/**
 * 声明动态表格行类
 * @param instance
 * @return
 */
var DyRow = function(instance){
	
	this.instance = instance;
	this.id = instance.id;
	this.dyTable = null;
	
};

/**
 * 声明动态单元格类
 * @param instance
 * @return
 */
var DyCell = function(instance){
	
	this.instance = instance;
	this.dyRow = null;
	
};


/**
 * 注册事件类型
 */
DyTable.prototype.eventType = {
		
		add : "add",
		del : "del",
		delAll : "delAll"
			
};

/******************* 表操作方法 start ********************************/
/**
 * 添加一行
 * @param clsName
 * @param id
 * @return
 */
DyTable.prototype.addRow = function(){

	var oRow = this.instance.insertRow(this.instance.rows.length);	
	oRow.id = this.generateId();
	oRow.className = this.config.trClassName;
	var dyRow = new DyRow(oRow);
	dyRow.dyTable = this;
	this.rows.push(dyRow);
	return dyRow;	
};

/**
 * 获得行
 * @param id
 * @return
 */
DyTable.prototype.getRow = function(id){
	var rows = this.rows;
	for(var i = 0; i < rows.length; i++){
		var oRow = rows[i];
		if(oRow.instance.id == id){
			return oRow;
		}
	}
	return null;
};

/**
 * 删除一行
 * @param trId
 * @return
 */
DyTable.prototype.delRow = function(id,delCallFun){
	
	var oRow = null;
	var rows = this.rows;
	var i;
	for(i = 0; i < rows.length; i++){
		oRow = rows[i];
		if(oRow.instance.id == id){
			break;
		}
	}
	oRow.instance.removeNode(true);
	
	while(i < rows.length -1){
		this.rows[i] = this.rows[i+1];
		i++;
	}
	this.rows.length = this.rows.length-1;
	
	if(delCallFun != null){
		delCallFun(oRow);
	}
	return oRow;
};

/**
 * 删除所有行
 * @param trId
 * @return
 */
DyTable.prototype.delAllRow = function(delCallFun){
	
	var oRow = null;
	var rows = this.rows;
	var i;
	for(i = 0; i < rows.length; i++){
		oRow = rows[i];
		oRow.instance.removeNode(true);
	}
	this.rows.length = 0;
	
	if(typeof(delCallFun) != "undefined"){
		delCallFun(oRow);
	}
	return true;
};

/**
 * 注册事件
 * @param type
 * @param id
 * @return
 */
DyTable.prototype.registerEvent = function(type,id,backFun){
	
	var obj = document.getElementById(id);
	if(obj == null){
		alert("这测事件时找不到"+id);
		return;
	}
	var dt = this;
	var oRowArr = new Array();
	if(type == this.eventType.add){ //新增行
		obj.onclick = function(){
			var oRow = dt.addFullRow();
			oRowArr.push(oRow);
			if(typeof(backFun) != "undefined"){
				if(oRowArr.length == 1){
					backFun(oRowArr[0]);
				}else{
					backFun(oRowArr);
				}
			}
		};
	}else if(type == this.eventType.delAll){  //删除选择行
		
		obj.onclick = function(){
              			
            var oRowNewArr = dt.delSelectedRow();
            oRowArr.concat(oRowNewArr);
        	if(typeof(backFun) != "undefined"){
        		if(oRowArr.length == 1){
        			backFun(oRowArr[0]);
        		}else{
        			backFun(oRowArr);
        		}
        	}
		};
	}

};


/**
 * 添加满行
 * @return
 */
DyTable.prototype.addFullRow = function(){
	  
	var dyRow = this.addRow();
	var rowId = dyRow.instance.id;
	for(var i = 0; i < this.config.tdHtml.length; i++){
		var dyCell = dyRow.addCell();
		var s = this.config.tdHtml[i];
		s = s.replace("\$\{rowId\}",rowId);
		dyCell.addHtml(s);
		
		//设置初始化值
		var valueList = this.config.tdHtmlValue;
		if(valueList != null && i != 0){
			var htmlValue = valueList[i-1];
			var type = htmlValue.split("|")[0];
			var value = htmlValue.split("|")[1];
            if(type == "moreInput"){
            	var values = value.split("#");
            	for(var j = 0; j < values.length; j++){
            		htmlValue = values[j];
        			type = htmlValue.split("|")[0];
        			value = htmlValue.split("|")[1];
        			dyCell.instance = this.getValueCell(dyCell.instance, type, value);
            	}
            }else{
            	dyCell.instance = this.getValueCell(dyCell.instance, type, value);
            }
		}
	}
	if(this.config.autoDelOp == true){  //添加删除操作
		var dyCell = dyRow.addCell();
		var delInput = "<input type='button' value='删除' onclick='"+this.obj_s+".delRow(\""+rowId+"\","+this.obj_s+".delCallFun)' class='minButton'>";
		dyCell.addHtml(delInput);
	}
	return dyRow;
};


DyTable.prototype.delSelectedRow = function(){
	
	var ids = this.getSelectIds(this.config.chkIds);
	var idstr = ids.split(",");
	if(idstr != null && idstr != ""){
		var oRowArr = new Array();
		for(var i = 0; i < idstr.length; i++){
			oRowArr.push(this.delRow(idstr[i]));
		}
		return oRowArr;
	}
	return null;
};


DyTable.prototype.getValueCell = function(oCell,type,value){
	switch(type){
		case 'checkbox': 
	         var chks = oCell.getElementsByTagName('input');
	         for(var j = 0; j < chks.length; j++){
	        	 var oChk = chks[j];
	        	 if(oChk.type == 'checkbox' && value == oChk.value){
	        		 oChk.checked = true;
	        	 }
	         };
	         break;
		case 'radio': 
	        var rads = oCell.getElementsByTagName('input');
	        for(var j = 0; j < rads.length; j++){
	       	 var oRad = rads[j];
	       	 if(oRad.type == 'radio' && value == oRad.value){
	       		oRad.checked = true;
	       		break;
	       	 }
	        };
			break;
		case 'select': 
			 var select = oCell.getElementsByTagName('select')[0];
			 var options = select.options;
			 for(var j = 0; j < options.length; j++){
				 var opt = options[j];
				 if(opt.value == value){
					 select.options[j].selected = true;
					 break;
				 }
			 };
			 break;
		default : oCell.firstChild.value = value ;break;
   }
	return oCell;
};
DyTable.prototype.refresh = function(){
	
};

/**
 * 获得checkbox 选择的 ids
 */
DyTable.prototype.getSelectIds = function(chkIds){
	
    var ids = "";
	var chkObjs = document.getElementsByName(chkIds);
	for(var i = 0; i < chkObjs.length; i++){
		if(chkObjs[i].checked){
             ids += chkObjs[i].value+",";
        }
    }
	if(ids != ""){
		ids = ids.substring(0,ids.length-1);
	}
	return ids;
};



/**
 * 生成ID
 * @return
 */
DyTable.prototype.generateId = function(){

	var randNum = "";
	for(var i = 0; i < 4; i++){
		
		var n = Math.random() * 9;  
		var r = Math.round(n); //获得0 到9 的随机数
		randNum = randNum + r;
	}
	return new Date().getTime() + randNum;
};


/******************* 表操作方法 end ********************************/

/******************* 行操作方法 start ********************************/
/**
 * 添加一个单元格
 */
DyRow.prototype.addCell = function(){

	var oRow = this.instance;
	var oCell = oRow.insertCell(oRow.cells.length);	
	oCell.id = this.dyTable.generateId();
	oCell.className = this.dyTable.config.tdClsName;
	var dyCell = new DyCell(oCell);
	dyCell.dyRow = this;
	return dyCell;
};


/**
 * 设置样式
 */
DyRow.prototype.setClsName = function(clsName){
	
	this.instance.className = clsName;
	
};

/******************* 行操作方法 end ********************************/



/******************* 单元格操作方法 start ********************************/
/**
 * 添加表单
 */
DyCell.prototype.addHtml = function(htmlStr){
	
	this.instance.innerHTML = htmlStr;
	
};


/******************* 单元格操作方法 end ********************************/














